package xcmg.device.service.warehouse;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xcmg.device.dao.entity.warehouse.AbnormalInDO;
import xcmg.device.dao.entity.warehouse.AbnormalInDetailDO;
import xcmg.device.dao.mapper.warehouse.AbnormalInDetailMapper;
import xcmg.device.dao.mapper.warehouse.AbnormalInMapper;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.split.SplitService;
import xcmg.device.service.vo.warehouse.AbnormalInDetailVO;
import xcmg.device.service.vo.warehouse.AbnormalInVO;
import yb.ecp.fast.feign.FastGenClient;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.util.ListUtil;
import yb.ecp.fast.infra.util.StringUtil;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class AbnormalInDetailService {

    @Autowired
    private AbnormalInDetailMapper abnormalInDetailMapper;

    @Autowired
    private AbnormalInMapper abnormalInMapper;

    @Autowired
    private AbnormalInService abnormalInService;

    @Autowired
    private FastGenClient fastGenClient;

    @Autowired
    private SplitService splitService;

   /**
    * @Author: WangKe
    * @Description: 新增非正常入库详情记录
    * @Date: 2018/1/25 0025
    */
    public ErrorCode add(AbnormalInDetailVO abnormalInDetailVO){
        ActionResult<String> textResult = fastGenClient.textGuid();
        if (textResult.getCode() != ErrorCode.Success.getCode())
        {
            return ErrorCode.IllegalArument;
        }
        AbnormalInDetailDO abnormalInDetailDO = new AbnormalInDetailDO();
        BeanUtils.copyProperties(abnormalInDetailVO, abnormalInDetailDO);

        abnormalInDetailDO.setId(textResult.getValue());
        abnormalInDetailDO.setState(0);
        abnormalInDetailDO.setCreateDate(new Date());
        abnormalInDetailDO.setRealQty(BigDecimal.ZERO);


        ErrorCode code = abnormalInDetailMapper.insert(abnormalInDetailDO)>0? ErrorCode.Success: ErrorCode.Failure;
        return code;
    }

    /**
     * @Author: WangKe
     * @Description: 根据非正常入库单号查询详情记录
     * @Date: 2018/1/25 0025
     */
    public List<AbnormalInDetailDO> findByAbnormalNo(String abnormalNo, String orgId){
        if(abnormalNo == null){
            return new ArrayList<AbnormalInDetailDO>();
        }
        List<AbnormalInDetailDO> list =  abnormalInDetailMapper.findByAbnormalNoForLocationUser(abnormalNo, orgId);
        if(CollectionUtils.isNotEmpty(list)){
            for(AbnormalInDetailDO model : list){
                if(model.getRealQty() == null){
                    model.setRealQty(BigDecimal.ZERO);
                }
            }
        }
        return list;
    }

    public List<AbnormalInDetailDO> findByAbnormalNos(List<String> orderList, String orgId){
        return abnormalInDetailMapper.findByAbnormalNos(orderList, orgId);
    }

    /**
     * @Author: WangKe
     * @Description: 根据入库单号删除入库详情记录
     * @Date: 2018/1/23 0023
     */
    public ErrorCode deleteByAbnormalNo(String abnormalNo, String orgId){
        return abnormalInDetailMapper.deleteByAbnormalNo(abnormalNo, orgId)>0? ErrorCode.Success: ErrorCode.WarehouseIdError;
    }

    /**
     * @Author: WangKe
     * @Description: 根据入库单号删除入库详情记录
     * @Date: 2018/1/26 0026
     */
    public ErrorCode deleteByAbnormalNoList(List<String> orderList, String orgId){
        return abnormalInDetailMapper.removeByInOrderList(orderList, orgId)>0? ErrorCode.Success: ErrorCode.WarehouseIdError;
    }

    /**
     * @Author: WangKe
     * @Description: 根据入库单号和零件号更新已打印条码数量
     * @Date: 2018/1/26 0026
     */
    public ErrorCode updatePrintNum(String abnormalNo, String materialsNo, BigDecimal printNum){
        if(abnormalNo == null || materialsNo == null || printNum == null || printNum.compareTo(BigDecimal.ZERO) == 0){
            return ErrorCode.AbnormalDetailArgumentError;
        }
        AbnormalInDetailVO vo = new AbnormalInDetailVO();
        vo.setAbnormalNo(abnormalNo);
        vo.setMaterialsNo(materialsNo);

        List<AbnormalInDetailDO> list = abnormalInDetailMapper.list(vo);
        if(ListUtil.isNullOrEmpty(list)){
            return ErrorCode.AbnormalDetailNotFound;
        }
        AbnormalInDetailDO entity = list.get(0);
        entity.setPrintNum(printNum);
        return abnormalInDetailMapper.updateByPrimaryKey(entity)>0? ErrorCode.Success: ErrorCode.Failure;
    }

    /**
     * @Author: WangKe
     * @Description: 新增入库记录时，更新非正常入库单和详情的状态
     * @Date: 2018/2/6 0006
     */
    public boolean updateByInbound(String abnormalNo, String materialNo, BigDecimal realAmount,
                                   Integer singelState, Integer totalState, String orgId, BigDecimal thisAmount)throws Exception{
        AbnormalInDetailVO detailVO = new AbnormalInDetailVO();
        detailVO.setAbnormalNo(abnormalNo);
        detailVO.setMaterialsNo(materialNo);
        detailVO.setOrgId(orgId);
        List<AbnormalInDetailDO> list = abnormalInDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return false;
        }
        AbnormalInDetailDO detailDO = list.get(0);
        if(realAmount.compareTo(detailDO.getQty()) > 0){
            throw new Exception("实际入库数量不能大于计划入库数量");
        }
        detailDO.setRealQty(realAmount);
        detailDO.setState(singelState);
        boolean success = abnormalInDetailMapper.updateByPrimaryKeySelective(detailDO)>0;
        if(success){
            AbnormalInVO inboundVO = new AbnormalInVO();
            inboundVO.setAbnormalNo(abnormalNo);
            inboundVO.setOrgId(orgId);
            List<AbnormalInDO> inboundList = abnormalInService.list(inboundVO);
            if(ListUtil.isNullOrEmpty(inboundList)){
                return false;
            }
            AbnormalInDO inboundDO = inboundList.get(0);
            inboundDO.setState(totalState);
            success = abnormalInService.update(inboundDO);

            if(success && thisAmount != null &&  !StringUtil.isNullOrEmpty(inboundDO.getSplitOrder())){
                splitService.updateInOutNum(inboundDO.getSplitOrder(), materialNo, thisAmount, 1, orgId);
            }
        }
        return success;
    }


    /**
     * @Author: WangKe
     * @Description: 无条码入库时更新事务单状态
     * @Date: 2018/4/4 0004
     */
    public boolean updateStateNoBarcode(String businessOrder, String materialNo, Integer state, String orgId){
        boolean success = abnormalInMapper.updateStateNoBarcode(state, businessOrder, orgId)>0;
        if(!success){
            return false;
        }
        success = abnormalInDetailMapper.updateStateNoBarcode(state, businessOrder, materialNo, orgId)>0;
        return success;
    }

    public AbnormalInDetailDO getAbnormalInDetailDO(String abnormalNo, String materialNo, String orgId)throws Exception{
        AbnormalInDetailVO detailVO = new AbnormalInDetailVO();
        detailVO.setAbnormalNo(abnormalNo);
        detailVO.setMaterialsNo(materialNo);
        detailVO.setOrgId(orgId);
        List<AbnormalInDetailDO> list = abnormalInDetailMapper.list(detailVO);
        if(ListUtil.isNullOrEmpty(list)){
            return null;
        }
        return list.get(0);
    }
}
